package logic

import (
	"context"
	"time"
	"zhongxc.cc/grpc-common/mall-product/types/product"

	"zhongxc.cc/mall-product-api/internal/svc"
	"zhongxc.cc/mall-product-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductLogic {
	return &ProductLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductLogic) ProductInfo(req *types.InfoRequest) (resp *product.InfoResponse, err error) {
	// todo: add your logic here and delete this line

	logx.Info("product info...")
	logx.Info(req)

	inReq := &product.InfoRequest{}
	inReq.Id = req.ProductId

	ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancelFunc()
	info, err := l.svcCtx.ProductRpc.Info(ctx, inReq)
	if err != nil {
		logx.Error(err)
		return nil, err
	}

	return info, nil
}
